package com.luych.toolbox.common.feign.view.base.service;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.luych.toolbox.common.feign.FeignView;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@ApiModel("用户信息")
public class UserView extends FeignView implements UserDetails {

    @ApiModelProperty("用户名")
    private String userName;
    @ApiModelProperty("登录名")
    private String loginName;
    @ApiModelProperty("密码")
    private String passWord;
    @ApiModelProperty("电话")
    private String tel;
    @ApiModelProperty("邮箱")
    private String email;
    @ApiModelProperty("头像ID")
    private String image;
    @ApiModelProperty("是否在线")
    private Boolean online;
    @ApiModelProperty("在线状态")
    private String onlineStatus;
    @ApiModelProperty("上次登录时间")
    private Date lastLoginTime;
    @ApiModelProperty("上次登录IP地址")
    private String lastLoginIp;
    @ApiModelProperty("用户所属角色列表")
    private List<RoleView> roles;
    @ApiModelProperty("用户拥有权限列表")
    private List<PermissionView> permissions;

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (permissions == null) {
            return new ArrayList<>();
        }
        return permissions.stream()
                .map(PermissionView::getPermissionValue)
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return passWord;
    }

    @Override
    @JsonIgnore
    public String getUsername() {
        return loginName;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public List<RoleView> getRoles() {
        return roles;
    }

    public void setRoles(List<RoleView> roles) {
        this.roles = roles;
    }

    public List<PermissionView> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<PermissionView> permissions) {
        this.permissions = permissions;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Boolean getOnline() {
        return online;
    }

    public void setOnline(Boolean online) {
        this.online = online;
    }

    public String getOnlineStatus() {
        return onlineStatus;
    }

    public void setOnlineStatus(String onlineStatus) {
        this.onlineStatus = onlineStatus;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    @JsonFormat(pattern = "yyyy-MM-dd")
    public Date getLastLoginDate() {
        return lastLoginTime;
    }

    public String getLastLoginIp() {
        return lastLoginIp;
    }

    public void setLastLoginIp(String lastLoginIp) {
        this.lastLoginIp = lastLoginIp;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
}
